import {createTeamApi, editAgrencyApi} from '../../../api/app';
import {getDate, convertDate, getTime} from '../../../utils/date';
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    teamName: '',
    node: '',
    desc: '',
    planList: [],
    daySelect: ['工作', '学习', '娱乐', '生活', '其他'], // 类型
    motionDayValue: 0,
    currentDate: getDate (new Date (), '-'),
    startDate: getDate (new Date (), '-'),
    address: '广州海珠区',
    isAdd: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const data = options.data ? JSON.parse (options.data) : '';
    if (data) {
      this.setData ({
        teamName: data.name,
        desc: data.content,
        startDate: data.creactDate,
        motionDayValue: data.sort,
        isAdd: false,
        id: data.id
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  chooseAddress () {
    wx.chooseAddress ({
      success: result => {
        console.log (result);
      },
      fail: err => {
        console.log (err);
      },
    });
  },
  toJoin () {
    wx.navigateTo ({
      url: '/subPages/joinTeam/index',
      success: result => {},
      fail: () => {},
      complete: () => {},
    });
  },
  // 选择日期
  selectStartDate (e) {
    const startDate = new Date (e.detail.value).getTime ();
    this.setData ({
      startDate: getDate (new Date (startDate), '-'),
    });
  },
  // 选择运动天数
  selectType (e) {
    const motionDayValue = Number (e.detail.value);
    this.setData ({
      motionDayValue,
    });
  },
  // 监听输入框改变
  bindinput (e) {
    const {currentTarget, detail} = e;
    let index = currentTarget.dataset.index;
    let value = detail.value;
    const list = this.data.planList;
    list[index].teamPlan = value;
    this.setData ({
      planList: list,
    });
    console.log (e);
  },
  // 提交
  async submit () {
    const uid = wx.getStorageSync ('openId') || '';
    console.log (this.data.planList);
    try {
      let res = null;
      if (this.data.isAdd) {
        res = await createTeamApi ({
          name: this.data.teamName,
          content: this.data.desc,
          sort: this.data.motionDayValue,
          createDate: this.data.startDate,
          status: 0,
          uid,
        });
      } else {
        res = await editAgrencyApi ({
          name: this.data.teamName,
          content: this.data.desc,
          sort: this.data.motionDayValue,
          createDate: this.data.startDate,
          status: 0,
          id: this.data.id
        });
      }
      if (res.code == 200) {
        wx.showToast ({
          title: this.data.isAdd ? '创建成功' : '编辑成功',
          icon: 'none',
        });
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      } else {
        wx.showToast ({
          title: this.data.isAdd ? '创建失败' : '编辑失败',
          icon: 'none',
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
});
